﻿using System;
using System.Windows;
using jSLApp.Client.ApplicationBase;
using jSLApp.Client.MainService;
using jSLApp.Client.Workflow;
using jSLCore.Client.Presentation.Authorization;
using jSLCore.Client.Presentation.BaseControl;
using jSLCore.Client.Presentation.BaseControl.Selection;

namespace jSLApp.Client.Forms
{
    public partial class PersonalPage : DialogControl
    {
        private readonly string _userName;

        public PersonalPage(string userName):this()
        {
            _userName = userName;
        }

        public PersonalPage()
        {
            InitializeComponent();
        }

        #region dialog

        public override bool IsOkVisible
        {
            get { return false; }
        }

        public override bool IsStandartOkCancelEnabled
        {
            get { return true; }
        }

        public override object CancelContent
        {
            get { return "Закрыть"; }
        }

        public override void LoadInitialData()
        {
            LoadUserName();
            LoadBalance();
            LoadCheckouts();
            LoadPayments();
        }

        private void LoadPayments()
        {
            DoWork("Загрузка оплат",(sender, args) =>
                                        {
                                            args.Result = Communication.Proxy.GetUserPayments(args.String);
                                        },(o, eventArgs) =>
                                                              {
                                                                  if(eventArgs.Error==null)
                                                                  {
                                                                      payGrid.ItemsSource = eventArgs.Result;
                                                                  }
                                                              },_userName);
        }

        private void LoadCheckouts()
        {
            DoWork("Загрузка покупок", (sender, args) =>
                                           {
                                               args.Result = Communication.Proxy.GetUserCheckouts(args.String);
                                           }, (o, eventArgs) =>
                                                                  {
                                                                      if (eventArgs.Error == null)
                                                                      {
                                                                          buyGrid.ItemsSource = eventArgs.Result;
                                                                      }
                                                                  },_userName);


        }

        private void LoadBalance()
        {
            DoWork("Загрузка баланса",(sender, args) =>
                                          {
                                              args.Result = Communication.Proxy.GetUserBalance(args.String);
                                          },(o, eventArgs) =>
                                                                {
                                                                    if(eventArgs.Error==null)
                                                                    {
                                                                        balanceTxt.Text = string.Format("{0} р.",
                                                                                                        eventArgs.Result);
                                                                    }
                                                                },_userName);
        }

        private void LoadUserName()
        {
            nameTxt.Text = _userName.Trim();
        }

        #endregion

        private void reload_Clicked(object sender, RoutedEventArgs e)
        {
            LoadInitialData();
        }

        private void pay_Clicked(object sender, RoutedEventArgs e)
        {
            var dlg = new PaymentModuleList(){SelectionMode = GridSelectionMode.SingleRow};
            dlg.ShowDialog(delegate(object o, object data)
                               {
                                   var module = (PaymentModule)data;
                                   DataHelper.Pay(module.FormatUriString,_userName,module.Description);
                               });
        }

        private void goFilm_Clicked(object sender, RowSelectedEventArgs e)
        {
            var checkOut = (CheckOut)e.RowObject;
            MainContentContainer.Current.ShowFilm(checkOut.FilmId);
        }
    }
}
